package cn.edu.sgu.www.mhxysy.service.xingyin.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.xingyin.XingyinViewAttributeDTO;
import cn.edu.sgu.www.mhxysy.entity.xingyin.XingyinViewAttribute;
import cn.edu.sgu.www.mhxysy.mapper.xingyin.XingyinViewAttributeMapper;
import cn.edu.sgu.www.mhxysy.pager.xingyin.XingyinViewAttributePager;
import cn.edu.sgu.www.mhxysy.service.xingyin.XingyinViewAttributeService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.vo.xingyin.XingyinViewAttributeVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class XingyinViewAttributeServiceImpl implements XingyinViewAttributeService {

    private final XingyinViewAttributeMapper xingyinViewAttributeMapper;

    @Autowired
    public XingyinViewAttributeServiceImpl(XingyinViewAttributeMapper xingyinViewAttributeMapper) {
        this.xingyinViewAttributeMapper = xingyinViewAttributeMapper;
    }

    @Override
    public void insert(XingyinViewAttributeDTO xingyinViewAttributeDTO) {
        XingyinViewAttribute xingyinViewAttribute = toEntity(xingyinViewAttributeDTO);

        xingyinViewAttribute.setId(null);

        xingyinViewAttributeMapper.insert(xingyinViewAttribute);
    }

    @Override
    public void deleteById(Integer id) {
        xingyinViewAttributeMapper.deleteById(id);
    }

    @Override
    public void updateById(XingyinViewAttributeDTO xingyinViewAttributeDTO) {
        ObjectUtils.requireNonNull(xingyinViewAttributeDTO.getId(), CustomMessages.ID_NOT_NULL);

        XingyinViewAttribute xingyinViewAttribute = toEntity(xingyinViewAttributeDTO);

        xingyinViewAttributeMapper.updateById(xingyinViewAttribute);
    }

    @Override
    public List<XingyinViewAttributeVO> selectByXingyinId(Integer xingyinId) {
        if (xingyinId == null) {
            return new ArrayList<>();
        }

        return xingyinViewAttributeMapper.selectByXingyinId(xingyinId);
    }

    @Override
    public Page<XingyinViewAttribute> selectByPage(XingyinViewAttributePager pager) {
        QueryWrapper<XingyinViewAttribute> queryWrapper = new QueryWrapper<>();
        Page<XingyinViewAttribute> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getType() != null,
                "type", pager.getType()
        );
        queryWrapper.eq(
                pager.getXingyinId() != null,
                "xingyin_id", pager.getXingyinId()
        );
        queryWrapper.eq(
                pager.getPropertyId() != null,
                "property_id", pager.getPropertyId()
        );

        return xingyinViewAttributeMapper.selectPage(page, queryWrapper);
    }

    private XingyinViewAttribute toEntity(XingyinViewAttributeDTO xingyinViewAttributeDTO) {
        XingyinViewAttribute xingyinViewAttribute = new XingyinViewAttribute();

        xingyinViewAttribute.setId(xingyinViewAttributeDTO.getId());
        xingyinViewAttribute.setMax(xingyinViewAttributeDTO.getMax());
        xingyinViewAttribute.setMin(xingyinViewAttributeDTO.getMin());
        xingyinViewAttribute.setType(xingyinViewAttributeDTO.getType());
        xingyinViewAttribute.setXingyinId(xingyinViewAttributeDTO.getXingyinId());
        xingyinViewAttribute.setPropertyId(xingyinViewAttributeDTO.getPropertyId());

        return xingyinViewAttribute;
    }

}